package com.tyk.bookstore.back.logistics.manager.excel.config;

import com.tyk.bookstore.back.common.core.excel.ExcelManager;
import com.tyk.bookstore.back.common.core.model.exception.CustomException;
import com.tyk.bookstore.back.common.core.model.result.CommonResultEnum;
import com.tyk.bookstore.back.common.core.util.AssertUtil;
import com.tyk.bookstore.back.logistics.manager.excel.model.ExpressCompanyExcelVo;
import com.tyk.bookstore.back.logistics.manager.excel.model.WaybillExcelVo;
import com.tyk.bookstore.back.logistics.model.entity.ExpressCompanyDo;
import com.tyk.bookstore.back.logistics.model.result.ExpressCompanyResultEnum;
import com.tyk.bookstore.back.logistics.repository.ExpressCompanyRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author tyk
 * @description LogisticsExcelConfig
 */
@Configuration
@RequiredArgsConstructor
public class LogisticsExcelConfig {

    private final ExpressCompanyRepository expressCompanyRepository;

    @Bean
    public ExcelManager<ExpressCompanyExcelVo> expressCompanyExcelManager() {
        return new ExcelManager<>() {
            @Override
            public void checkRow(ExpressCompanyExcelVo data) {
                // 物流公司不存在
                AssertUtil.isFalse(
                        expressCompanyRepository.exist1Eq(
                                ExpressCompanyDo::getBriefName, data.getBriefName(), ExpressCompanyDo.class
                        ),
                        ExpressCompanyResultEnum.EXPRESS_COMPANY_ALREADY_EXISTS
                );
            }

            @Override
            public void handleSaveException(MultipartFile file, Exception e) {
                throw new CustomException(CommonResultEnum.FILE_OR_DATA_FORMAT_ERROR);
            }
        };
    }

    @Bean
    public ExcelManager<WaybillExcelVo> waybllExcelManager() {
        return new ExcelManager<>() {
        };
    }
}
